package rope;

class Branch implements Rope {
	// Represents the concatenation of left and right.
	// left and right are non-null.
	// Rep invariant: length is the sum of the lengths
	//   of left and right
	Rope left, right;
	int length;


	public Branch(Rope l, Rope r) {
		left = l; right = r;
		length = l.length() + r.length();
	}
	public int length() {
		return length;
	}
	public String toString() {
		// could make this faster with a StringBuffer
		// at the cost of complexity.
		return left.toString() + right.toString();
	}

	public char get(int i) {
		if (i < left.length()) return left.get(i);
		else return right.get(i - left.length());
	}

	public void put(int i, char c) {
		if (i < left.length()) left.put(i, c);
		else right.put(i, c);
	}

	public Rope concat(Rope r) {
		return new Branch(this, r);
	}


}
